package config

import (
	"fmt"
	amqp "github.com/rabbitmq/amqp091-go"
	"github.com/spf13/viper"
	"log"
)

var (
	user     = viper.GetString("rabbitmq.user")
	password = viper.GetString("rabbitmq.password")
	host     = viper.GetString("rabbitmq.host")
	port     = viper.GetInt("rabbitmq.port")
)

// InitAMQPConnection 初始化AMQP连接
func InitAMQPConnection() *amqp.Connection {

	conn, err := amqp.Dial(fmt.Sprintf("amqp://%s:%s@%s:%d/", user, password, host, port))
	if err != nil {
		log.Fatalf("Failed to connect to RabbitMQ: %v", err)
	}
	return conn
}

// GetAMQPChannel 获取AMQP消息
func GetAMQPChannel() (*amqp.Connection, *amqp.Channel) {

	conn, err := amqp.Dial(fmt.Sprintf("amqp://%s:%s@%s:%d/", user, password, host, port))
	if err != nil {
		log.Fatalf("Failed to connect to RabbitMQ: %v", err)
	}
	ch, err := conn.Channel()
	if err != nil {
		log.Fatalf("Failed to open a channel: %v", err)
	}
	return conn, ch
}
